<?php

define('IN_APP', true);
define('DIRECTORY_SEPARATOR', '/');

gt_base::auto_load_func();
/**
 * Class gt_base
 * 基础性功能类，全局引用
 */
class gt_base{

    public static function load_class(){

    }

    public static function load_module_class($class){
        $module = MODULE_NAME;
        $file_path = APP_BASE.'Application'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'classes';
        $file_path = dir_path($file_path);
        if(!is_dir($file_path)){
            return LANG('module_no_classes');
        }
        $class_ext = '.class.php';
        if(!file_exists($file_path.$class.$class_ext)){
            return LANG('module_no_this_file');
        }
        require($file_path.$class.$class_ext);
        return new $class;
    }

    public static function load_func(){

    }

    /**
     * @param $classesname
     * @return bool
     * 加载系统函数库
     */
    public static function load_sys_func($func){
        if(empty($func))
            return false;
        $file_path = APP_BASE.'lib'.DIRECTORY_SEPARATOR.'function'.DIRECTORY_SEPARATOR.$func.'.func.php';
        if(is_dir($file_path)){
            return false;
        }
        if(!file_exists($file_path)){
            return false;
        }
        return require_once($file_path);
    }

    public static function load_config(){

    }

    public static function load_model($model=''){
        return self::_load_model($model);
    }

    public static function _load_class(){

    }

    public static function auto_load_func($path = ''){
        self::_auto_load_func($path);
    }

    /**
     * @param string $path //默认卢静为:/lib/function/autolaodfunc
     * 加载自动函数库
     */
    private static function _auto_load_func($path = ''){
        if(empty($path))
            $path = APP_BASE.'lib'.DIRECTORY_SEPARATOR.'function'.DIRECTORY_SEPARATOR.'autolaodfunc';
        $path .= DIRECTORY_SEPARATOR.'*.func.php';
        $auto_func = glob($path);
        if(!empty($auto_func) && is_array($auto_func)){
            foreach($auto_func as $v){
                include $v;
            }
        }
    }

    /**
     * @param string $model
     * @return bool|Model|\Think\Model
     * 返回加载的model
     * 先判断当前模块是否有，再判断公用模块是否有
     */
    private static function _load_model($model = ''){
        if(empty($model))
            return false;
        $common_path = APP_BASE.'Application'.DIRECTORY_SEPARATOR.'Common'.DIRECTORY_SEPARATOR.'Model'.DIRECTORY_SEPARATOR;
        $module_path = APP_BASE.'Application'.DIRECTORY_SEPARATOR.MODULE_NAME .DIRECTORY_SEPARATOR.'Model'.DIRECTORY_SEPARATOR;
//        $common_path = dir_path($common_path);
//        $module_path = dir_path($module_path);
        if(!is_dir(dir_path($common_path)))
            return false;
        if(!is_dir(dir_path($module_path)))
            return false;
        $model_paht = $model.'Model.class.php';
        if(file_exists($common_path.$model_paht) || file_exists($module_path.$model_paht)){
            return D($model);
        }
        if(!file_exists($common_path.$model_paht) && !file_exists($module_path.$model_paht)){
            return M($model);
        }
        return false;
    }

}